import {
    iosProvinces,
    iosCitys,
    iosCountys
} from "@/components/share/area/areaData_v2.js";
 
export default function(Vue) {
    //省份
    Vue.filter("province", function(val) {
        var reg = new RegExp("[\\u4E00-\\u9FFF]+","g");
        if(reg.test(val)){
            return val;
        }
        if (val&&val!= null&& val!= 'undefined') {
            for(var i=0;i<iosProvinces.length;i++){
                if(iosProvinces[i].id==val){
                    return iosProvinces[i].value;
                }
            };
        }
    });
    //市份
    Vue.filter("city", function(val) {
        var reg = new RegExp("[\\u4E00-\\u9FFF]+","g");
        if(reg.test(val)){
            return val;
        }
        if (val&&val!= null&& val!= 'undefined') {
            for(var i=0;i<iosCitys.length;i++){
                if(iosCitys[i].id==val){
                    return iosCitys[i].value;
                }
            };
        }
    });
    //区
    Vue.filter("district", function(val) {
        var reg = new RegExp("[\\u4E00-\\u9FFF]+","g");
        if(reg.test(val)){
            return val;
        }
        if (val&&val!= null&& val!= 'undefined') {
            for(var i=0;i<iosCountys.length;i++){
                if(iosCountys[i].id==val){
                    return iosCountys[i].value;
                }
            };
        };
    });
    //yuyu 2017-07-25 全局商品图片过滤
    Vue.filter("filterImg", function(val) {
        if (val != null && val != "null") {
            if (
                val[0] != null &&
                val != "null" &&
                JSON.parse(val)[0] != null &&
                JSON.parse(val)[0] != "null" &&
                JSON.parse(val)[0].Mobile
            ) {
                return JSON.parse(val)[0].Mobile.Url + JSON.parse(val)[0].Mobile.Name;
            } else {
                return "/static/img/none.jpg";
            }
        } else {
            return "/static/img/none.jpg";
        }
    });
    Vue.filter("filterImgUrl", function(val) {
      if (val != null && val != "null" && val && val != '[]'&& val != '[null,null,null,null]') {
        // console.log(val);
        if(typeof(val) == 'string'){
          try {
            val = JSON.parse(val);
          }
          catch(err) {
            return `https://image.csyaoly.com${val}`
          }
        }
        if (val.length == 1) {
            return `https://image.csyaoly.com${val[0]}`;
        } else {
          if (typeof (val) == 'object') {
            return val.Mobile.Url + val.Mobile.Name;
          } else {
            if (!val[0].Mobile) {
              return val[0];
            } else {
              return val[0].Mobile.Url + val[0].Mobile.Name;
            }
          }
        };
      } else {
        return "/static/img/none.jpg";
      }
    });

    Vue.filter("simpleImgUrl", function(val) {
        if (val != null && val != "null" && val) {
            return "https://image.csyaoly.com" + val;
        }
    });
    Vue.filter("ContactImg", function(val) {
        if (val != null && val != "null") {
            if (
                val[0] != null &&
                val != "null" &&
                JSON.parse(val)[0] != null &&
                JSON.parse(val)[0] != "null"
            ) {
                return JSON.parse(val)[0].Mobile.Url + JSON.parse(val)[0].Mobile.Name;
            } else {
                return "/images/myfile_img.png";
            }
        } else {
            return "/images/myfile_img.png";
        }
    });
    // 时间轴
    Vue.filter("time", function(value) {
        // var fmt = "yyyy-MM-dd hh:mm:ss";
        var fmt = "yyyy-MM-dd";
        var date = new Date(value);
        var o = {
            "M+": date.getMonth() + 1, //月份
            "d+": date.getDate(), //日
            "h+": date.getHours(), //小时
            "m+": date.getMinutes(), //分
            "s+": date.getSeconds(), //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            S: date.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(
                RegExp.$1,
                (date.getFullYear() + "").substr(4 - RegExp.$1.length)
            );
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(
                    RegExp.$1,
                    RegExp.$1.length == 1 ?
                    o[k] :
                    ("00" + o[k]).substr(("" + o[k]).length)
                );
        return fmt;
    });

    // 时间轴
    Vue.filter("timeHurs", function(value) {
        // var fmt = "yyyy-MM-dd hh:mm:ss";
        var fmt = "hh:mm";
        var date = new Date(value);
        var o = {
            "M+": date.getMonth() + 1, //月份
            "d+": date.getDate(), //日
            "h+": date.getHours(), //小时
            "m+": date.getMinutes(), //分
            "s+": date.getSeconds(), //秒
            "q+": Math.floor((date.getMonth() + 3) / 3), //季度
            S: date.getMilliseconds() //毫秒
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(
                RegExp.$1,
                (date.getFullYear() + "").substr(4 - RegExp.$1.length)
            );
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(
                    RegExp.$1,
                    RegExp.$1.length == 1 ?
                    o[k] :
                    ("00" + o[k]).substr(("" + o[k]).length)
                );
        return fmt;
    });

    // 时间轴
    Vue.filter("timeshort", function(value) {
        return value ? value.replace('T', ' ') : '';
    });
    // 数字向上取整
    Vue.filter("ceilNum", function(val) {
        return val ? Math.ceil(val) : "";
    });
    // 医院等级
    Vue.filter("rank", function(val) {
        if (!val) {
            return "";
        }
        switch (val) {
            case "1":
                return "一级";
                break;
            case "2":
                return "二级";
                break;
            case "3":
                return "三级";
                break;
        }
    });
    Vue.filter("grade", function(val) {
        if (!val) {
            return "";
        }
        switch (val) {
            case "1":
                return "甲等";
                break;
            case "2":
                return "乙等";
                break;
            case "3":
                return "丙等";
                break;
        }
    });
    //金额格式化
    Vue.filter("fmoney", function(num) {
        if (!num) { return '0' }
        num = num.toString().replace(/\$|\,/g, "");
        if (isNaN(num)) num = "0";
        var sign = num == (num = Math.abs(num));
        num = Math.floor(num * 100 + 0.50000000001);
        var cents = num % 100;
        num = Math.floor(num / 100).toString();
        if (cents < 10) cents = "0" + cents;
        for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
            num =
            num.substring(0, num.length - (4 * i + 3)) +
            "," +
            num.substring(num.length - (4 * i + 3));
        return (sign ? "" : "-") + num + "." + cents;
    });
    Vue.filter("jsDateDiff", function(dateTimeStamp) {
        if (!dateTimeStamp) return '';
        dateTimeStamp = dateTimeStamp.split("T")[0];
        dateTimeStamp = Date.parse(dateTimeStamp);
        var minute = 1000 * 60;
        var hour = minute * 60;
        var day = hour * 24;
        var halfamonth = day * 15;
        var month = day * 30;
        var newsd = new Date();
        var today = newsd.getFullYear() + '/' + (newsd.getMonth() + 1) + '/' + newsd.getDate();
        var now = new Date(today).getTime();
        var diffValue = now - dateTimeStamp;
        var monthC = diffValue / month;
        var weekC = diffValue / (7 * day);
        var dayC = diffValue / day;
        var hourC = diffValue / hour;
        var minC = diffValue / minute;
        var result = "";
        if (diffValue < 0) {
            // if (-monthC >= 1) {
            //     result = "" + parseInt(-monthC) + "个月后";
            // } else if (-weekC >= 1) {
            //     result = "" + parseInt(-weekC) + "周后";
            // } else 
            if (-dayC >= 1) {
                result = "" + parseInt(-dayC) + "天后";
            } else result = "今天";
            return result;
        }
        if (monthC >= 1) {
            result = "" + parseInt(monthC) + "月前";
        } else if (weekC >= 1) {
            result = "" + parseInt(weekC) + "周前";
        } else if (dayC >= 1) {
            result = "" + parseInt(dayC) + "天前";
        }
        // else if (hourC >= 1) {
        //     result = "" + parseInt(hourC) + "小时前";
        // } else if (minC >= 1) {
        //     result = "" + parseInt(minC) + "分钟前";
        // }
        else result = "今天";
        return result;
    });

    function getDateTimeStamp(dateStr) {
        return Date.parse(dateStr.replace(/-/gi, "/"));
    }

    Vue.filter("role", function(val) {
        if (!val) {
            return "";
        }
        switch (val) {
            case "Salesman":
                return "业务员";
                break;
            case "Hospital":
                return "客户";
                break;
            case "Pharmacy":
                return "药店";
                break;
            case "Warehouse":
                return "仓管";
                break;
            case "BillingClerk":
                return "开票员";
                break;
            case "ServiceManager":
                return "业务经理";
                break;
            case "Driver":
                return "配送员";
                break;
            case "Dispatch":
                return "运输调度";
                break;
        }
    });

    Vue.filter("billstate", function(val) {
        if (!val) {
            return "";
        }
        switch (val) {
            case "P01":
                return "待提交";
                break;
            case "P02":
                return "待受理";
                break;
            case "P03":
                return "待出库";
                break;
            case "P04":
                return "部分出库";
                break;
            case "P05":
                return "全部出库";
                break;
            case "P06":
                return "业务经理";
                break;
            case "P07":
                return "已完结";
                break;
            case "P08":
                return "待出库";
                break;
            case "P09":
                return "已提货";
                break;
            case "P10":
                return "已交货";
                break;
            case "P11":
                return "缺货处理中";
                break;
        }
    });
    Vue.filter("Trsupplierhvpostate", function(val) {
        if (!val) {
            return "";
        }
        switch (val) {
            case "N":
                return "待提交";
                break;
            case "Y":
                return "已提交";
                break;
        }
    });
    Vue.filter("PoState", (val) => {
      if (!val) {
        return "";
      }
      switch (val) {
        case "11":
          return "初始";
          break;
        case "12":
          return "提交";
          break;
        case "20":
          return "运营接单";
          break;
        case "21":
          return "物流集单中";
          break;
        case "22":
          return "物流派单中";
          break;
        case "23":
          return "物流派单确认";
          break;
        case "29":
          return "物流派单撤单";
          break;
        case "30":
          return "仓库入库交接";
          break;
        case "31":
          return "仓储派单受理";
          break;
        case "32":
          return "仓储派单拒单";
          break;
        case "39":
          return "仓库出库交接";
          break;
        case "41":
          return "配送派单受理";
          break;
        case "42":
          return "排车";
          break;
        case "43":
          return "提货中";
          break;
        case "44":
          return "配送中";
          break;
        case "49":
          return "配送派单拒单";
          break;
        case "51":
          return "已受理";
          break;
        case "52":
          return "提货交接";
          break;
        case "53":
          return "物流中转交接";
          break;
        case "55":
          return "集货交接";
          break;
        case "59":
          return "司机拒单";
          break;
        case "56":
          return "送货交接";
          break;
        case "81":
          return "到货签收";
          break;
      }
    });
}